import { Hero } from '@libs/db/models/hero.model';
import { Body, Controller, Delete, Get, Param, Put, Post, Query } from '@nestjs/common';
import { ApiOperation, ApiProperty, ApiTags } from '@nestjs/swagger';
import { ModelType } from '@typegoose/typegoose/lib/types';
import { InjectModel } from 'nestjs-typegoose';

class herosPostDto {
  @ApiProperty({description: '英雄名称'})
  name: string;  
  @ApiProperty({description: '英雄称号'})
  title: string;
  @ApiProperty({description: '英雄职位'})
  position: [];
  @ApiProperty({description: '头像'})
  icon: string;
  @ApiProperty({description: '英雄展示图'})
  img: string;
  @ApiProperty({description: '英雄技能'})
  skill: [{
    sname: string;
    scontent: string;
    sicon: string;     // 图标
    sconsume: string;  // 消耗
    scooling: string; 
  }];
  @ApiProperty({description: '英雄打星'})
  cover: {
    difficulty: Number;  // 难度
    cskill: Number;      // 技能
    attack: Number;      // 攻击
    existence: Number;   // 生存
  };
  @ApiProperty({description: '顺风出装'})
  equip1: [];
  @ApiProperty({description: '逆风出装'})
  equip2: [];
  @ApiProperty({description: '使用技巧'})
  usageTips: string;
  @ApiProperty({description: '对抗技巧'})
  battleTips: string;
  @ApiProperty({description: '团战思路'})
  teamTips: string;
  @ApiProperty({description: '搭档英雄'})
  parteners: [{
    hero: string;
    description: string;
  }];
  @ApiProperty({description: '被谁克制'})
  beRestrained: [{
    hero: string;
    description: string;
  }];
  @ApiProperty({description: '克制英雄'})
  restrainWho: [{
    hero: string;
    description: string;
  }];
}

@ApiTags('英雄接口')
@Controller('heros')
export class HerosController {
  constructor(@InjectModel(Hero) private readonly HeroModel: ModelType<Hero>) {}

  @Get('page/')
  @ApiOperation({summary: '英雄列表'})
  async selectAll(@Query() pages: any) {
    // 如果没有 name，就是模糊查询，就取默认值 '', 查询全部。
    let { page, pagesize, sort, name = '' } = pages;

    const total = await this.HeroModel.countDocuments({$or: [{'name': {'$regex': name}}]});

    const skip: number = page * pagesize - pagesize;
    pagesize = pagesize - 0;

    const data = await this.HeroModel.find({$or: [{'name': {'$regex': name}}]}).skip(skip).limit(pagesize).sort({_id: sort}).populate(['equip1', 'equip2']);

    return {
      total,
      content: data
    }
  }

  @Get(':id')
  @ApiOperation({summary: '根据id查找英雄'})
  async selectId(@Param('id') id: string) {
    try {
      return await this.HeroModel.findById(id).populate(['equip1', 'equip2']);
    } catch (err) {
      return { success: '创建失败' + err }
    }
  }

  @Post()
  @ApiOperation({summary: '创建英雄'})
  async create(@Body() herosPostDto: herosPostDto) {
    try {
      const data = await this.HeroModel.create(herosPostDto);
      return { success: '创建成功' }
    } catch(Err) {
      console.log(Err)
      return { success: '创建失败' + Err }
    }
  }

  @Put(':id')
  @ApiOperation({summary: '修改英雄'})
  async update(@Param('id') id: string, @Body() herosPostDto) {
    try {
      await this.HeroModel.findByIdAndUpdate(id, herosPostDto);
      return { success: '修改成功' }
    } catch(Err) {
      return { success: '修改失败' + Err }
    }
  }

  @Delete(':id')
  @ApiOperation({summary: '删除英雄'})
  async remove(@Param('id') id: string) {
    try {
      await this.HeroModel.findByIdAndDelete({_id: id});
      return { success: '删除成功' }
    } catch (Err) {
      return { success: '删除失败' + Err }
    }
  }

}
